package bt.servlet.postulante;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bt.dao.PostulanteDAO;
import bt.excepcion.DAOExcepcion;
import bt.modelo.Postulante;

/**
 * Servlet implementation class PostulanteInsertarServlet
 */
@WebServlet("/PostulanteInsertarServlet")
public class PostulanteInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostulanteInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String a = request.getParameter("nombre");
		String b = request.getParameter("apellido");
		String c = request.getParameter("clave");
		int d = 0;
		if(request.getParameter("sexo").equals("Hombre")) {
			d = 1;
		}	else {
					d=2;
				}
		String f = request.getParameter("fecnac");
		String g = request.getParameter("pais") ;
		int p = 0;
		if (g.compareTo("Peru")==0) {
			p = 1;
		} else if (g.compareTo("Argentina")==0) {
					p = 2;
				} else if (g.compareTo("Colombia")==0) {
							p = 3;
						} else if (g.compareTo("Brasil")==0) {
									p = 4;
								} else  {
											p = 5;
										}
		int h = 0;
		if(request.getParameter("EstadoCivil").equals("Soltero")) {
			h = 1;
		}	else if(request.getParameter("EstadoCivil").equals("Casado")) {
			     	h = 2;
		     	}	else if(request.getParameter("EstadoCivil").equals("Viudo")) {
		     				h = 3;
			      		}	else if(request.getParameter("EstadoCivil").equals("Divorciado")) {
			      					h = 4;
			      				} else	{
			      						h=5;
			      						}
		String i = request.getParameter("emailp");
		String j = request.getParameter("emails");
		String k = request.getParameter("direccion");
		String l = request.getParameter("dni");
		String m = request.getParameter("telefono");
		String n = request.getParameter("celular");
		String o = request.getParameter("discapacitado");
		int q = 0;
		if (o.compareTo("on")==0) {
			q = 1;
		}
		int t = 0;
		if(request.getParameter("estado").equals("Activo")) {
			t = 1;
		}	else {
					t = 2;
				}
		Postulante r = new Postulante();
		r.setNombre(a);
		r.setApellido(b);
		r.setPassword(c);
		r.setIdSexo(d);
		r.setFechaNacimiento(f);
		r.setIdPaisNacimiento(p);
		r.setIdEstadoCivil(h);
		r.setEmailPrincipal(i);
		r.setEmailSecundario(j);
		r.setDireccion(k);
		r.setDni(l);
		r.setTelefonoCasa(m);
		r.setTelefonoMovil(n);
		r.setDiscapacitado(q);
		r.setEstado(t);
		r.setIdPais(1);
		r.setIdDepartamento(2);	 
		r.setFechaRegistro("2011-06-22");
		
		PostulanteDAO dao = new PostulanteDAO();
		try {
			dao.insertar(r);
			response.sendRedirect(request.getContextPath()
					+ "/PostulanteBuscarServlet");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
